package com.ruoyi.union.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.union.domain.SysActivityRegistration;
import com.ruoyi.union.mapper.SysActivityRegistrationMapper;
import com.ruoyi.union.service.ISysActivityRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 活动报名Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class SysActivityRegistrationServiceImpl extends ServiceImpl<SysActivityRegistrationMapper, SysActivityRegistration> implements ISysActivityRegistrationService
{
    @Autowired
    private SysActivityRegistrationMapper sysActivityRegistrationMapper;

    /**
     * 查询活动报名
     *
     * @param registrationId 活动报名主键
     * @return 活动报名
     */
    @Override
    public SysActivityRegistration selectSysActivityRegistrationByRegistrationId(Long registrationId)
    {
        return sysActivityRegistrationMapper.selectSysActivityRegistrationByRegistrationId(registrationId);
    }

    /**
     * 查询活动报名列表
     *
     * @param sysActivityRegistration 活动报名
     * @return 活动报名
     */
    @Override
    public List<SysActivityRegistration> selectSysActivityRegistrationList(SysActivityRegistration sysActivityRegistration)
    {
        return sysActivityRegistrationMapper.selectSysActivityRegistrationList(sysActivityRegistration);
    }

    /**
     * 新增活动报名
     *
     * @param sysActivityRegistration 活动报名
     * @return 结果
     */
    @Override
    public int insertSysActivityRegistration(SysActivityRegistration sysActivityRegistration)
    {
        sysActivityRegistration.setRegistrationTime(DateUtils.getNowDate());
        return sysActivityRegistrationMapper.insertSysActivityRegistration(sysActivityRegistration);
    }

    /**
     * 修改活动报名
     *
     * @param sysActivityRegistration 活动报名
     * @return 结果
     */
    @Override
    public int updateSysActivityRegistration(SysActivityRegistration sysActivityRegistration)
    {
        return sysActivityRegistrationMapper.updateSysActivityRegistration(sysActivityRegistration);
    }

    /**
     * 批量删除活动报名
     *
     * @param registrationIds 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteSysActivityRegistrationByRegistrationIds(Long[] registrationIds)
    {
        return sysActivityRegistrationMapper.deleteSysActivityRegistrationByRegistrationIds(registrationIds);
    }

    /**
     * 删除活动报名信息
     *
     * @param registrationId 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteSysActivityRegistrationByRegistrationId(Long registrationId)
    {
        return sysActivityRegistrationMapper.deleteSysActivityRegistrationByRegistrationId(registrationId);
    }

    /**
     * 会员报名活动
     *
     * @param activityId 活动ID
     * @param memberId 会员ID
     * @return 结果
     */
    @Override
    public boolean registerActivity(Long activityId, Long memberId)
    {
        // 检查是否已报名
        if (checkRegistrationExists(activityId, memberId)) {
            return false;
        }

        SysActivityRegistration registration = new SysActivityRegistration();
        registration.setActivityId(activityId);
        registration.setMemberId(memberId);
        registration.setRegistrationStatus("0"); // 待确认
        registration.setRegistrationTime(DateUtils.getNowDate());

        return insertSysActivityRegistration(registration) > 0;
    }

    /**
     * 取消报名
     *
     * @param activityId 活动ID
     * @param memberId 会员ID
     * @return 结果
     */
    @Override
    public boolean cancelRegistration(Long activityId, Long memberId)
    {
        return sysActivityRegistrationMapper.deleteSysActivityRegistrationByActivityIdAndMemberId(activityId, memberId) > 0;
    }

    /**
     * 根据活动ID查询报名列表
     *
     * @param activityId 活动ID
     * @return 报名列表
     */
    @Override
    public List<SysActivityRegistration> selectSysActivityRegistrationByActivityId(Long activityId)
    {
        return sysActivityRegistrationMapper.selectSysActivityRegistrationByActivityId(activityId);
    }

    /**
     * 根据会员ID查询报名列表
     *
     * @param memberId 会员ID
     * @return 报名列表
     */
    @Override
    public List<SysActivityRegistration> selectSysActivityRegistrationByMemberId(Long memberId)
    {
        return sysActivityRegistrationMapper.selectSysActivityRegistrationByMemberId(memberId);
    }

    /**
     * 检查会员是否已报名该活动
     *
     * @param activityId 活动ID
     * @param memberId 会员ID
     * @return 是否已报名
     */
    @Override
    public boolean checkRegistrationExists(Long activityId, Long memberId)
    {
        SysActivityRegistration registration = sysActivityRegistrationMapper.checkRegistrationExists(activityId, memberId);
        return registration != null;
    }

    /**
     * 确认报名
     *
     * @param registrationId 报名ID
     * @return 结果
     */
    @Override
    public boolean confirmRegistration(Long registrationId)
    {
        SysActivityRegistration registration = selectSysActivityRegistrationByRegistrationId(registrationId);
        if (registration != null) {
            registration.setRegistrationStatus("1"); // 已确认
            return updateSysActivityRegistration(registration) > 0;
        }
        return false;
    }

    /**
     * 根据活动ID统计报名人数
     *
     * @param activityId 活动ID
     * @return 报名人数
     */
    @Override
    public int countRegistrationsByActivityId(Long activityId)
    {
        LambdaQueryWrapper<SysActivityRegistration> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysActivityRegistration::getActivityId, activityId);
        return (int) count(wrapper);
    }
}
